package com.ruoyi.web.controller.system;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

import com.ruoyi.common.annotation.Excel;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.utils.file.Filebyte;
import com.ruoyi.system.domain.TmcActivityUser;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.TmcCommunityActivity;
import com.ruoyi.system.service.ITmcCommunityActivityService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

/**
 * 活动信息Controller
 * 
 * @author ruoyi
 * @date 2021-03-31
 */
@RestController
@RequestMapping("/system/activity")
public class TmcCommunityActivityController extends BaseController
{
    @Autowired
    private ITmcCommunityActivityService tmcCommunityActivityService;

    /**
     * 查询活动信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(TmcCommunityActivity tmcCommunityActivity)
    {
        startPage();
        List<TmcCommunityActivity> list = tmcCommunityActivityService.selectTmcCommunityActivityList(tmcCommunityActivity);
        return getDataTable(list);
    }
    /**
     * 小程序查询活动信息列表
     */
    @GetMapping("/queryActivity")
    public TableDataInfo queryActivity()
    {
        startPage();
        List<TmcCommunityActivity> list = tmcCommunityActivityService.queryActivity();
        return getDataTable(list);
    }
    /**
     * 导出活动信息列表
     */
    @Log(title = "活动信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TmcCommunityActivity tmcCommunityActivity)
    {
        List<TmcCommunityActivity> list = tmcCommunityActivityService.selectTmcCommunityActivityList(tmcCommunityActivity);
        ExcelUtil<TmcCommunityActivity> util = new ExcelUtil<TmcCommunityActivity>(TmcCommunityActivity.class);
        return util.exportExcel(list, "activity");
    }

    /**
     * 小程序获取活动信息详细信息
     */
    @GetMapping("/selectActivityById")
    public AjaxResult selectActivityById(Long id,Long publicuserId)
    {
        return AjaxResult.success(tmcCommunityActivityService.selectActivityById(id,publicuserId));
    }

    /**
     * 获取活动信息详细信息
     */
    //@PreAuthorize("@ss.hasPermi('system:activity:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(tmcCommunityActivityService.selectTmcCommunityActivityById(id));
    }

    /**
     * 查询活动参与人
     */
    @GetMapping("/selectPerson")
    public AjaxResult selectPerson(Long id)
    {
        return AjaxResult.success(tmcCommunityActivityService.selectPerson(id));
    }

    /**
     * 删除活动参与人
     */
    @DeleteMapping("/deletePerson")
    public AjaxResult deletePerson(Long id,Long publicuserId)
    {
        return AjaxResult.success(tmcCommunityActivityService.deletePerson(id,publicuserId));
    }

    /**
     * 新增活动信息
     */
    @Log(title = "活动信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TmcCommunityActivity tmcCommunityActivity)
    {
        return AjaxResult.success("新增成功",tmcCommunityActivityService.insertTmcCommunityActivity(tmcCommunityActivity));
    }

    // 上传图片
    @Log(title = "图片", businessType = BusinessType.INSERT)
    @PostMapping(value = "/upload")
    @CrossOrigin
    public AjaxResult pictureUpload(MultipartFile file,Long id) {
        try {
            String profile = RuoYiConfig.getProfile();
            byte[] bytes = file.getBytes();
            //文件夹
            Path path2 = Paths.get(profile+"//" + id);
            //具体文件
            Path path = Paths.get(profile+"//" + id+ "//" + file.getOriginalFilename());
            Path path1 = Paths.get("profile//" + id + "//" + file.getOriginalFilename());
            File file1 = path.toFile();
            //创建文件夹
            if (!file1.exists()) {
                path2.toFile().mkdirs();
            }
            if (file1.isFile()) {
                String suffixName = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
                String replace = path1.toString().replace(suffixName, "(" + (int) (Math.random() * 10) + ")" + suffixName);
            }
            Files.write(path, bytes);//创建文件
            TmcCommunityActivity tmcCommunityActivity = new TmcCommunityActivity();
            tmcCommunityActivity.setImageurl(path1.toString());
            tmcCommunityActivity.setId(id);
            if (tmcCommunityActivityService.updateTmcCommunityActivity(tmcCommunityActivity) > 0) {
                return AjaxResult.success("文件上传成功");
            } else {
                if (file1.isFile()) {
                    file1.delete();
                }
                return AjaxResult.error("文件上传失败");
            }
        } catch (IOException e) {
            e.printStackTrace();
            return AjaxResult.error("文件上传失败");
        }
    }
    /**
     * 修改活动信息
     */
    @Log(title = "活动信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TmcCommunityActivity tmcCommunityActivity)
    {
        return toAjax(tmcCommunityActivityService.updateTmcCommunityActivity(tmcCommunityActivity));
    }

    /**
     * 删除活动信息
     */
    @Log(title = "活动信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tmcCommunityActivityService.deleteTmcCommunityActivityByIds(ids));
    }
    /**
     * 报名参加活动
     */
    @PostMapping("/signUp")
    public AjaxResult signUp(@RequestBody TmcActivityUser tmcActivityUser)
    {
        return AjaxResult.success("报名成功",tmcCommunityActivityService.signUp(tmcActivityUser));
    }
    /**
     * 查询用户参与的活动
     */
    @GetMapping("/participate")
    public AjaxResult selectParticipate(Long publicuserId)
    {
        return AjaxResult.success(tmcCommunityActivityService.selectParticipate(publicuserId));
    }
}
